package com.walkline.util.ui;

import com.walkline.util.GPATools;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.BitmapField;

public class MyEmtionBitmapField extends BitmapField
{
	private static boolean needSmallIcon = Display.getWidth() <= 480 ? true : false;

	public MyEmtionBitmapField(String[] emotion)
	{
		super(needSmallIcon ? GPATools.ResizeTransparentBitmap(Bitmap.getBitmapResource(emotion[1]), 47, 47, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_STRETCH) : Bitmap.getBitmapResource(emotion[1]), BitmapField.FOCUSABLE);
		setCookie(emotion[0]);
	}

	protected void paintBackground(Graphics g)
	{
		super.paintBackground(g);

		if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
		{
			g.setColor(0x1896cf);
			g.drawRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
		}
	}

	protected void drawFocus(Graphics g, boolean on)
	{
		if(on)
		{
			moveButton();

			g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
			paintBackground(g);
		}
	}

    protected boolean keyChar(char character, int status, int time) 
    {
        if (character == Characters.ENTER)
        {
            clickButton();
            return true;
        }

        return super.keyChar(character, status, time);
    }

    protected boolean navigationClick(int status, int time) 
    {
        clickButton(); 
        return true;    
    }

    protected boolean trackwheelClick(int status, int time)
    {        
        clickButton();    
        return true;
    }

    protected boolean invokeAction(int action) 
    {
        switch(action)
        {
            case ACTION_INVOKE:
                clickButton(); 
                return true;
        }

        return super.invokeAction(action);
    }

    public void clickButton() {fieldChangeNotify(0);}

    public void moveButton() {fieldChangeNotify(2);}

    protected boolean touchEvent(TouchEvent message)
    {
        int x = message.getX(1);
        int y = message.getY(1);

        if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {return false;}

        switch (message.getEvent())
        {
        	case TouchEvent.CLICK:
                clickButton();
                return true;
        }

        return super.touchEvent(message);
    }
}